package com.wunu.smartlock1.activity;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;
import android.widget.TextView;

import com.wunu.smartlock1.R;
import com.wunu.smartlock1.net.response.LockQrCode;
import com.wunu.smartlock1.util.CommonUtil;

import java.lang.ref.WeakReference;


/**
 * 二维码
 * Created by admin on 2018/4/21.
 */

public class QrcodeActivity_qiguai extends AppCompatActivity{
    ImageView pic;//锁二维码
    ImageView qrcode;//锁二维码

    TextView username;//
    private static final int LOAD_SUCESS = 0x1;
    private final MyHandler myHandler = new MyHandler(this);
    Bitmap bitmap;
    private String act;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.smartlock_qrcode);
        pic = findViewById(R.id.pic);
        qrcode = findViewById(R.id.qrcode);
        username = findViewById(R.id.username);
        String info = getIntent().getStringExtra("param");
        System.out.println("二维码页面收到的数据:"+info);
        final LockQrCode qrCodeData = CommonUtil.gson.fromJson(info,LockQrCode.class);
        System.out.println(""+qrCodeData.getName());
        System.out.println(""+qrCodeData.getAvatar());
        System.out.println(""+qrCodeData.getQrCodeUrl());
        System.out.println("qrCodeData:"+qrCodeData);

        username.setText(qrCodeData.getName());

        new Thread(new Runnable() {
            @Override
            public void run() {
                try{
                    act = "qrCode";
                    bitmap = CommonUtil.getImageBitmap(qrCodeData.getQrCodeUrl());
                    // 通过handler把bitmap传出去
                    Message msg = myHandler.obtainMessage(LOAD_SUCESS,bitmap);
                    myHandler.sendMessage(msg);

                    act = "pic";
                    bitmap =CommonUtil.getImageBitmap(qrCodeData.getAvatar());
                    // 通过handler把bitmap传出去
                    msg = myHandler.obtainMessage(LOAD_SUCESS,bitmap);
                    myHandler.sendMessage(msg);
                }catch (Exception e){
                    System.out.println("加载二维码失败"+e.getMessage());
                }
            }
        }).start();


    }

    private class MyHandler extends Handler{
        private final WeakReference<QrcodeActivity_qiguai> weakReference;   // 软引用
        public MyHandler(QrcodeActivity_qiguai mainActivity){
            weakReference = new WeakReference<QrcodeActivity_qiguai>(mainActivity);
        }
        @Override
        public void handleMessage(Message msg) {
            QrcodeActivity_qiguai mainActivity = weakReference.get();
            if (mainActivity!=null){
                if(msg.what == LOAD_SUCESS){
                    if ("pic".equals(act))
                        mainActivity.pic.setImageBitmap((Bitmap) msg.obj); // 获取msg 设置UI显示
                    if ("qrCode".equals(act)) {
                        mainActivity.qrcode.setImageBitmap((Bitmap) msg.obj); // 获取msg 设置UI显示
                    }
                }
            }
        }
    }
}
